package dianmobile.eistarandroid.network.request;

import android.content.Context;

import com.android.volley.Response;

import org.xmlpull.v1.XmlPullParser;

import java.util.HashMap;
import java.util.Map;

public class LoginRequest extends ByhhAndroidRequest {

    public static void execute(Context context, Map<String, String> params, RequestCallback callback) {
        executeSuper(context, RequestType.LOGIN, params, callback);

    }

    public LoginRequest(int method, String url, Map<String, String> params,
                        RequestCallback callback, Response.ErrorListener listener) {
        super(method, url, params, callback, listener);

    }

    public static Map<String, String> createParams(String account, String password) {
        Map<String, String> params = new HashMap<>();
        params.put("id", account);
        params.put("pw", password);
        params.put("xml", "1");

        return params;
    }


    @Override
    protected void parseXmlToMap(XmlPullParser parser, Map<String, Object> result) {
        result.put("result", "yes");
        Map<String, String> cookiesMap = new HashMap<>();

        // data
        final String SU_UTMPNUM = "utmpnum";
        final String SU_UTMPUSERID = "utmpuserid";
        final String SU_UTMPKEY = "utmpkey";
        final String SU_INVISIBLE = "invisible";
        final String SU_CONTDAYS = "contdays";

        final String ERROR = "error";

        try {
            int even = parser.getEventType();
            String temp;
            while (even != XmlPullParser.END_DOCUMENT) {
                switch (even) {
                    case XmlPullParser.START_DOCUMENT:
                        break;

                    case XmlPullParser.START_TAG:
                        String tagName = parser.getName();
                        /*-----------------------------------------------
                        /-------  Login Success Response Data
                        /------------------------------------------------*/
                        if (SU_UTMPNUM.equals(tagName)) {
                            temp = parser.nextText();
                            result.put(SU_UTMPNUM, temp);
                            cookiesMap.put(SU_UTMPNUM, temp);
                        }
                        else if(SU_UTMPUSERID.equals(tagName)) {
                            temp = parser.nextText();
                            result.put(SU_UTMPUSERID, temp);
                            cookiesMap.put(SU_UTMPUSERID, temp);
                        }
                        else if (SU_UTMPKEY.equals(tagName)) {
                            temp = parser.nextText();
                            result.put(SU_UTMPKEY, temp);
                            cookiesMap.put(SU_UTMPKEY, temp);
                        }
                        else if (SU_INVISIBLE.equals(tagName)) {
                            temp = parser.nextText();
                            result.put(SU_INVISIBLE, temp);
                            cookiesMap.put(SU_INVISIBLE, temp);
                        }
                        else if (SU_CONTDAYS.equals(tagName)) {
                            temp = parser.nextText();
                            result.put(SU_CONTDAYS, temp);
                            cookiesMap.put(SU_CONTDAYS, temp);
                        }

                        /*-----------------------------------------------
                        /-------  Login Failed Response Data
                        /------------------------------------------------*/
                        else if (ERROR.equals(tagName)) {
                            result.put(ERROR, parser.nextText());
                        }

                        break;

                    case XmlPullParser.END_TAG:
                        break;
                }

                even = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }

        result.put(COOKIES, cookiesMap);
    }
}
